/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.yuhanginfo.modules.sys.web;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.yuhanginfo.common.config.Global;
import com.yuhanginfo.common.persistence.Page;
import com.yuhanginfo.common.web.BaseController;
import com.yuhanginfo.common.utils.StringUtils;
import com.yuhanginfo.modules.sys.entity.Store;
import com.yuhanginfo.modules.sys.entity.StoreWarehouse;
import com.yuhanginfo.modules.sys.entity.User;
import com.yuhanginfo.modules.sys.entity.Waybill;
import com.yuhanginfo.modules.sys.service.StoreService;
import com.yuhanginfo.modules.sys.service.StoreWarehouseService;
import com.yuhanginfo.modules.sys.service.WaybillService;
import com.yuhanginfo.modules.sys.utils.UserUtils;

/**
 * 门店仓库管理Controller
 * @author yangs
 * @version 2017-09-12
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/storeWarehouse")
public class StoreWarehouseController extends BaseController {

	@Autowired
	private StoreWarehouseService storeWarehouseService;
	@Autowired
	private StoreService storeService;
	@Autowired
	private WaybillService waybillService;
	
	@ModelAttribute
	public StoreWarehouse get(@RequestParam(required=false) String id) {
		StoreWarehouse entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = storeWarehouseService.get(id);
		}
		if (entity == null){
			entity = new StoreWarehouse();
		}
		return entity;
	}
	
	@RequiresPermissions("sys:storeWarehouse:view")
	@RequestMapping(value = {"list", ""})
	public String list(StoreWarehouse storeWarehouse, HttpServletRequest request, HttpServletResponse response, Model model) {
		User us = UserUtils.getUser();
		storeWarehouse.setSysUserId(us.getId());
		Page<StoreWarehouse> page = storeWarehouseService.findPage(new Page<StoreWarehouse>(request, response), storeWarehouse);
		List<Store> store=storeService.storeUserList(us.getId());
		model.addAttribute("store", store);
		model.addAttribute("page", page);
		return "modules/sys/storeMessage/storeWarehouseList";
	}

	@RequiresPermissions("sys:storeWarehouse:view")
	@RequestMapping(value = "form")
	public String form(StoreWarehouse storeWarehouse, Model model) {
		User us = UserUtils.getUser();
		List<Store> store=storeService.storeUserList(us.getId());
		model.addAttribute("store", store);
		model.addAttribute("storeWarehouse", storeWarehouse);
		return "modules/sys/storeMessage/storeWarehouseForm";
	}

	@RequiresPermissions("sys:storeWarehouse:edit")
	@RequestMapping(value = "save")
	public String save(StoreWarehouse storeWarehouse, Model model, RedirectAttributes redirectAttributes) {
		storeWarehouse.setCreateDate(new Date());
		User us = UserUtils.getUser();
		storeWarehouse.setCreateBy(us);
		if (!beanValidator(model, storeWarehouse)){
			return form(storeWarehouse, model);
		}
		storeWarehouseService.save(storeWarehouse);
		return "redirect:"+Global.getAdminPath()+"/sys/storeWarehouse/?repage";
	}
	
	
	@ResponseBody
	@RequiresPermissions("sys:storeWarehouse:view")
	@RequestMapping(value = "yzCode")
	public Map<String,Object> yzCode(String code,String id) {
		StoreWarehouse warehouse=new StoreWarehouse();
		warehouse.setCode(code);
		List<StoreWarehouse> list=storeWarehouseService.selectByCode(warehouse);
		Map<String,Object> map=new HashMap<>();
		if(id!=""&&id!=null){
			if(list.size()>1 && list.get(0).getId().equals(id)){
				map.put("st", 1);
			}
			if(!list.isEmpty() && !list.get(0).getId().equals(id)){
				map.put("st", 1);
			}
		}else{
			if(!list.isEmpty()){
				map.put("st", 1);
			}
		}
		return map;
	}
	
	
	@RequiresPermissions("sys:storeWarehouse:edit")
	@RequestMapping(value = "delete")
	public String delete(StoreWarehouse storeWarehouse, RedirectAttributes redirectAttributes) {
		List<Waybill> list=waybillService.selectByWarehouseId(storeWarehouse.getId());
		if(!list.isEmpty()){
			addMessage(redirectAttributes, "尚存订单从该仓库发货，删除失败!");
		}else{
			addMessage(redirectAttributes, "删除仓库成功");
		}
		
		return "redirect:"+Global.getAdminPath()+"/sys/storeWarehouse/?repage";
	}

}